/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vue;

import com.trolltech.qt.gui.QBrush;
import com.trolltech.qt.gui.QFrame;
import com.trolltech.qt.gui.QGraphicsScene;
import com.trolltech.qt.gui.QGraphicsView;
import com.trolltech.qt.gui.QPixmap;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import Interfaces.InterfacePopulation;
import Entites.Espece;
import Controleur.Population;

/**
 *
 * @author JeanW
 */
public class Monde extends QGraphicsView {

    private QGraphicsScene scene;
    private Signal0 stop = new Signal0();
    private Signal0 start = new Signal0();
    private InterfacePopulation population = new Population();
    private List<PEspece> listPEspece = new ArrayList<>();
    public Signal0 mondeChange = new Signal0();

    public InterfacePopulation getPopulation() {
        return population;
    }

    public void setPopulation(InterfacePopulation population) {
        this.population = population;
    }

    private void initMonde() {
        scene = new QGraphicsScene(this);
        scene.setSceneRect(0, 0, 1000, 1000);
        scene.setItemIndexMethod(QGraphicsScene.ItemIndexMethod.NoIndex);
        this.setScene(scene);
        setCacheMode(new QGraphicsView.CacheMode(QGraphicsView.CacheModeFlag.CacheBackground));

        setBackgroundBrush(new QBrush(new QPixmap(
                "img/sol.png")));
        setCacheMode(new QGraphicsView.CacheMode(
                QGraphicsView.CacheModeFlag.CacheBackground));
        setDragMode(QGraphicsView.DragMode.ScrollHandDrag);
        setViewportUpdateMode(QGraphicsView.ViewportUpdateMode.FullViewportUpdate);

        setFrameStyle(QFrame.Shape.NoFrame.value());
        setTransformationAnchor(QGraphicsView.ViewportAnchor.AnchorUnderMouse);
        setResizeAnchor(QGraphicsView.ViewportAnchor.AnchorViewCenter);
    }

    public void mondeChanged(Espece es) {
        //System.out.println("change");
        population.supprimerEspece(es);
        mondeChange.emit();
    }

    public void mondeChanged2() {
        for (PEspece c : listPEspece) {
            for (PEspece d : listPEspece) {
                if (c.status == true && d.status == true) {
                    if (c == d) {
                        continue;
                    }
                    if (c.boundingRect().intersects(d.boundingRect())) {
                        System.out.println(c.uneEspece.getNom() + " en collision avec " + d.uneEspece.getNom());
                        c.status = false;
                        d.status = false;
                    }
                }
            }
        }
    }

    public void mondeChangedVie() {
        mondeChange.emit();
    }

    public Monde(int nbPopulation) {

        initMonde();


        population.genererPopulation(nbPopulation);

        for (Espece es : population.getUnePopulation()) {
            es.getUnEsprit().getNom();
            switch (es.toString().length()) {
                case 5:
                    listPEspece.add(new PTroll(es));
                    break;
                case 4:
                    listPEspece.add(new PElfe(es));
                    break;

                case 8:
                    listPEspece.add(new PMage(es));
                    break;
                default:
                    break;
            }
        }

        for (PEspece c : listPEspece) {

            Random alea = new Random();
            int r1 = 0 + alea.nextInt(1001);
            int r2 = 0 + alea.nextInt(1001);
            c.setPos(r1, r2);

            scene.addItem(c);
            c.mort.connect(this, "mondeChanged(Espece)");
            c.moveSign.connect(this, "mondeChangedVie()");
            c.collision.connect(this, "mondeChanged2()");
        }

    }

    public void changeVitesse(int v) {
        for (PEspece c : listPEspece) {
            //c.setSpeed(v);
        }
    }
}
